<style>

{{>overlay-css}}

/* Example Layout CSS */
.overlay-example {
    position: relative;
    height: 450px;
    width: 550px;
}

#constrain-box {
    background-color: #F0F7F0;
    border: 1px solid #DBE3DB;

    position: relative;

    top: 80px;
    left: 80px;

    width: 350px;
    height: 300px;
}

.yui3-slider {
    position: absolute;
    background-color: transparent;
}

#x {
    position: absolute;
    top: 10px;
    left: 30px;
}

#y {
    position: absolute;
    top: 30px;
    left: 10px;
}

</style>

<div class="intro">
    <p>
    This example shows how you can use Overlay's constrained positioning support to constrain the XY position of the overlay so that it remains within another node on the page (or within the viewport).
    </p>
</div>

<div class="example">
    {{>overlay-constrain-source}}
</div>

<h2>Overlay Constrained XY Positioning</h2>

<h3>Setting Up The YUI Instance</h3>

<p>
As with the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, to create an instance of an Overlay on your page, the only module you need to request is the `overlay` module. The `overlay` module will pull in the `widget`, `widget-stack`, `widget-position`, `widget-position-align`, `widget-position-constrain` and `widget-stdmod` extensions it uses.
</p>

```javascript
YUI({...}).use("overlay", function (Y) {
    // We'll write example code here, where we have a Y.Overlay class
    // available, which is bundled with XY positioning, align and
    // constrain support.
});
```

<p>
Using the `overlay` module, will also pull down the default CSS required for overlay, on top of which we only need to add our required look/feel CSS for the example.
</p>

{{>need-skin-note}}
```
<body class="yui3-skin-sam"> {{>need-skin-comment}}
```

<h3>Instantiating The Overlay</h3>

<p>
For this example, we'll instantiate an Overlay from script, as we did for the <a href="overlay-align.html">"Alignment Support"</a> example, but use the `render` attribute to specify the node under which we want the Overlay to be rendered in the DOM, and to indicate that we want it rendered on construction. The `render` attribute is a sugar attribute for the `render()` method:
</p>

```javascript
// Create Overlay from script.
overlay = new Y.Overlay({
    id: "overlay",

    width : "140px",
    height: "120px",

    headerContent: "Constrained",
    bodyContent  : "Use the sliders to move the overlay",
    footerContent: '<label><input type="checkbox" id="constrain"> Constrain </label>',

    constrain: "#constrain-box",
    align    : {
        node  : "#constrain-box",
        points: ["cc", "cc"]
    },

    render: "#overlay-example"
});
```

<p>
We align the overlay to the center of the `#constrain-box` node, which we're also using as the constraining node for the overlay. The `constrain` attribute accepts a node reference (either an actual Node instance, or a string selector reference), or it can simply be set to `true` to constrain the overlay to the Viewport.
</p>

<h3>Demonstrating Constrained Support</h3>

<p>
For this example, we set up a couple of Slider instances which can be used to set the Overlay's `x` and `y` attribute values.
</p>

```javascript
// Get the region for the constraining box.
var constrainRegion = Y.one("#constrain-box").get("region");

// Use the Overlay's current `x` and `y` to set the initial Slider values.

// Create horizontal Slider.
sx = new Y.Slider({
    id    : "x",
    length: "450px",
    min   : constrainRegion.left - 50,
    max   : constrainRegion.right + 50,
    value : overlay.get("x"),
    render: "#overlay-example"
});

// Create vertical Slider.
sy = new Y.Slider({
    id    : "y",
    axis  : 'y',
    length: "400px",
    min   : constrainRegion.top - 50,
    max   : constrainRegion.bottom + 50,
    value : overlay.get("y"),
    render: "#overlay-example"
});
```

<p>
We set the `min` and `max` values of the slider instances to allow the overlay to be moved beyond the edge of the constraining region, and set the initial `value` of the sliders to reflect the current centered position of the overlay.
</p>

<p>
Finally, we set up `valueChange` listeners for the sliders, when attempt to set the corresponding axis position of the overlay:
</p>

```javascript
// Set the Overlay's `x` attribute value.
sx.after("valueChange", function (e) {
    overlay.set("x", e.newVal);
});

// Set the Overlay's `y` attribute value.
sy.after("valueChange", function (e) {
    overlay.set("y", e.newVal);
});
```

<h3>CSS: Overlay Look/Feel</h3>

<p>
As mentioned in the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, the overlay.css Sam Skin file (build/overlay/assets/skins/sam/overlay.css) provides the default functional CSS for the overlay. Namely the CSS rules to hide the overlay, and position it absolutely. However there's no default out-of-the-box look/feel applied to the Overlay widget.
</p>

<p>
The example provides it's own look and feel for the Overlay, by defining rules for the content box, header and body sections:
</p>

```
{{>overlay-css}}
```

<h2>Complete Example Source</h2>

```
{{>overlay-constrain-source}}
```
